import 'dart:developer';

import 'package:FlutterDemo/book_store_manager.dart';
import 'package:FlutterDemo/sample_plugin.dart';
import 'package:flutter/material.dart';

/// https://www.jianshu.com/p/c38dd2efa64a
class Plugin1ParticalWidget extends StatefulWidget {
  const Plugin1ParticalWidget({Key? key}) : super(key: key);

  @override
  _Plugin1ParticalWidgetState createState() => _Plugin1ParticalWidgetState();
}

class _Plugin1ParticalWidgetState extends State<Plugin1ParticalWidget> {
  String result = "";
  List<String> pkgList = <String>[];

  execBookSearchs() {
    StoreApi().search("google").then((value) {
      log("execBookSearchs sucess");
    });
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      // 获取Android 返回的版本名称
      platformVersion = await SamplePlugin.platformVersion ?? "Unknown platform version";
    } catch (e) {
      platformVersion = "Failed to get platform version.";
    }

    if (!mounted) return;

    setState(() {
      // 更新状态
      result = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: SingleChildScrollView(
        child: Column(
          children: [
            RaisedButton(
              onPressed: _loadAppVersion,
              child: Text("获取手机版本号"),
            ),
            RaisedButton(
              onPressed: _queryBook,
              child: Text("图书信息查询-有bug，待修复"),
            ),
            RaisedButton(
              onPressed: _findAllBookNames,
              child: Text("查询全部书名"),
            ),
            RaisedButton(
              onPressed: _loadAppPkgList,
              child: Text("获取应用列表"),
            ),
            buildList()
            // Expanded(child: buildList())
          ],
        ),
      ),
    );
  }

  void _loadAppVersion() {
    initPlatformState();
  }

  void _queryBook() {
    StoreApi().search('a').then((value) {
      pkgList.clear();
      setState(() {
        value.forEach((element) {
          pkgList.add("name: ${element!.title!} , author: ${element.author!}");
        });
      });
    });
  }

  void _findAllBookNames() {
    StoreApi().getBookNames().then((value) {
      setState(() {
        pkgList.clear();
        // 第一种方式
        // List<String> l2 = List<String>.from(value).cast<String>();
        // pkgList.addAll(value);

        // 第二种方式
        value.forEach((element) {
          pkgList.add(element!);
        });
      });
    });
  }

  void _loadAppPkgList() {
    SamplePlugin.getPkgList.then((value) {
      log("message data size = ${value.length}");
      setState(() {
        pkgList.clear();

        pkgList.addAll(value);
      });
    });
  }

  Widget buildList() {
    return ListView.separated(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("${pkgList[index]}"),
          );
        },
        separatorBuilder: (context, index) {
          return Divider();
        },
        itemCount: pkgList.length);
  }
}
